package com.vrp3d.domain.validator;

import com.vrp3d.common.enums.CommonEnum;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义验证邮箱注解
 *
 * @author vrp3d
 */
public class EmailValidator implements ConstraintValidator<Email, String> {

    /**
     * 邮箱的正则表达式.
     */
    private static Pattern pattern = Pattern.compile(CommonEnum.EMAIL_REGULAR.getMsg());

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtils.isNotBlank(value)) {
            Matcher m = pattern.matcher(value);
            return m.matches();
        } else {
            return false;
        }
    }

    @Override
    public void initialize(Email constraintAnnotation) {
    }
}